<--- %%NOBANNER%% --> substrm.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Similar to substr;                                                 |
| Note: this is a data step function, not a macro function;          |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| Argument:                                                          |
|    invar: the input var;                                           |
|    outvar: the output var;                                         |
|    start: a number, start position;                                |
|           Note: a negative value, will start from the end;         |
|    len: the length of the return string;                           |
|---------------<-- End of Files Arguments Needed-->-----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example:                                                           |
|    data one;                                                       |
|       x='this is a test'; output;                                  |
|       x='123456789';      output;                                  |
|       x='Test';           output;                                  |
|    run;                                                            |
|    data two;                                                       |
|       set one;                                                     |
|       %substrm(x, y, 1, 2);                                        |
|       %substrm(x, z, -2, 1);                                       |
|    run;                                                            |
|    %print(two);                                                    |
| Usage: substrm(invar, outvar, start, len);                         |
\-------------------<-- End of Files Created-->---------------------*/
%macro substrm(invar, outvar, start, len);
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  12-28-2001 9:12pm;                |
| Modified: 3-11-2002 9:52pm;                 |
| Purpose:  Substring;                        |
\--------------------------------------------*/
%if (%quote(&invar) eq) or (%quote(&outvar) eq) or (%quote(&invar) = %quote(&outvar)) %then %do;
   %put ==> Alert! I need both an input variable and an output variable!;
   abort 10;
%end;
%if (%quote(&start) eq) %then %do;
   %put ==> Alert! I need a start position!;
   abort 11;
%end;
%else %if (%index(%quote(&start),.)) %then %do;
   %put ==> Alert! Start position must be an integer!;
   abort 12;
%end;
%if (%quote(&len) ne) and (%index(%quote(&start),.)) %then %do;
   %put ==> Alert! Length must be an integer!;
   abort 13;
%end;
%if &start>0 %then %do;
   if 00 then &outvar=substr(&invar, &start, &len);
   end;
%end;
%else %if &start<0 %then %do;
   if 00 then &outvar=substr(&invar, (length(&invar)+2-abs(&start)-&len), &len);
   end;
%end;
%else %do;
   %put ==> Alert! Start position cannot be 0!;
   abort 14;
%end;
%mend substrm;